package router

import (
	"bluebell_qimi/controller"
	_ "bluebell_qimi/docs"
	"bluebell_qimi/logger"
	"bluebell_qimi/middlewares"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	gs "github.com/swaggo/gin-swagger"
	"net/http"
	"time"
)

func SetupRouter(mode string) *gin.Engine {
	if mode == gin.ReleaseMode {
		gin.SetMode(gin.ReleaseMode)
	}
	r := gin.New()
	r.Use(logger.GinLogger(),
		logger.GinRecovery(true),
		middlewares.Cors(),
		middlewares.RateLimitMiddleware(time.Second*2, 1))

	r.GET("/swagger/*any", gs.WrapHandler(swaggerFiles.Handler))
	r.GET("/ping", func(c *gin.Context) {
		//如果是登录的用户,判断请求头中是否有有效的JTW
		c.String(http.StatusOK, "ping success")
	})

	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": 404,
		})
	})

	v1 := r.Group("/api/v1")
	//注册业务路由
	v1.POST("/signup", controller.SignUpHandler)
	//登录路由
	v1.GET("/login", controller.LoginHandler)

	//获取所有帖子列表和详细信息
	v1.GET("/posts", controller.GetPostListHandler)
	//根据时间或者分数获取帖子列表，按热度或者时间排序，也有根据社区查帖子
	//如果是127.0.0.1:8084/api/v1/posts2?order=score&page=1&size=2，则没有按社区查
	v1.GET("/posts2", controller.GetPostListHandler2)

	//升级版可以根据时间或者热度查找社区（可带id）帖子
	v1.GET("/posts3", controller.GetCommunityPostListHandler)

	v1.Use(middlewares.JWTAuthMiddleware()) //应用JWT认证中间件
	{
		//获取社区列表
		v1.GET("/community", controller.CommunityHandler)
		//根据communityid获取社区详情
		v1.GET("/community/:id", controller.CommunityDetailHandler)

		//发帖
		v1.POST("/post", controller.CreatePostHandler)
		//根据社区id查帖子
		v1.GET("/post/:id", controller.GetPostDetailHandler)

		//投票
		v1.POST("/vote", controller.PostVoteController)
	}

	return r
}
